const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const mysql = require('mysql')
const conn = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'root',
    database:'001node_mysql'
})
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())

const cors = require('cors')
app.use(cors())
//查询所有英雄
app.get('/getallhero',(req,res) => {
    conn.query('select * from heros',(err,result) => {
        if(err) return res.send({status:500,msg:errmassage,data:null})
        res.send({status:200,msg:'ok',data:result})    
    })
})
//添加
app.post('/addhero',(req,res) => {
    const hero = req.body
    console.log(hero);
    
    const dt = new Date()
    const y = dt.getFullYear()
    const m = (dt.getMonth()+1).toString().padStart(2,'0')
    const d = dt.getDate().toString().padStart(2,'0')

    const hh = dt.getHours().toString().padStart(2,'0')
    const mm = dt.getMinutes().toString().padStart(2,'0')
    const ss = dt.getSeconds().toString().padStart(2,'0')

    hero.ctime = y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' +ss
    const sql = 'insert into heros set ?'
    conn.query(sql,hero,(err,result) => {
        if(err) return res.send({status:500,msg:err.massage,data:null})
        res.send({status:200,msg:'ok',data:null})
    })

})
app.listen(5001,() => {
    console.log('http://127.0.0.1:5001')    
})